package dtec.project.whereareu;

import java.util.ArrayList;

import org.w3c.dom.Document;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class Mapdraw extends FragmentActivity {
	private GoogleMap myMap;
	public UserLocation db;
	public UserData udb;
	public GPSTracker gps;
	public void GenDirection(LatLng fromLatLng,LatLng toLatLng,String curname,String friendname){			
			DirectionGen direction = new DirectionGen();
			Document doc = direction.getDocument(fromLatLng,toLatLng, DirectionGen.MODE_DRIVING);

//		 
//			 /*
//			 int duration = direction.getDurationValue(doc);
//			 String distance = direction.getDistanceText(doc);
//			 String start_address = direction.getStartAddress(doc);
//			 String copy_right = direction.getCopyRights(doc);
//			 */
//
			ArrayList<LatLng> directionPoint = direction.getDirection(doc);
			PolylineOptions rectLine = new PolylineOptions().width(2).color(Color.BLUE);
			
			for(int i = 0 ; i < directionPoint.size() ; i++){			
				rectLine.add(directionPoint.get(i));			
			}			
			myMap.addPolyline(rectLine);
			myMap.addMarker(new MarkerOptions()
	        .position(fromLatLng)                            
	        .title(curname)
	        .snippet("contents"));
			myMap.addMarker(new MarkerOptions()
	        .position(toLatLng)                            
	        .title(friendname)
	        .snippet("content"));
		  		
	}//end GenDirection	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mapdraw);
		//receive uid from other file
		int uid=getIntent().getIntExtra("recid", 0);
		gps = new GPSTracker(this);
		db=new UserLocation(this);
		udb=new UserData(this);
		//link map
		 SupportMapFragment mySupportMapFragment = 
				 (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
		 myMap = mySupportMapFragment.getMap();		 		
		// initial part to Zoom
		 myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(),gps.getLongitude()),8));
		 //initial Map type
		 myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		 String uname = udb.getUsername(),friendname = db.getName(uid);

	  	 GenDirection(new LatLng(gps.getLatitude(),gps.getLongitude()), db.getLocation(uid),uname,friendname);
	}


}
